@echo off
chcp 65001 >nul 2>&1
title DataPlexus Data Lineage System

:: 简化输出，避免编码问题
echo.

echo ================================================================
echo                    DataPlexus Startup Manager
echo                  Data Lineage Analysis System v1.0.0
echo ================================================================
echo.

:menu
echo Please select an option:
echo.
echo   1. Start Full System (Frontend + Backend)
echo   2. Start Backend Only
echo   3. Start Frontend Only
echo   4. Install Dependencies
echo   5. Environment Check
echo   6. Build Frontend
echo   7. Clean Cache
echo   8. Exit
echo.
set /p choice=Enter option (1-8):

if "%choice%"=="1" goto start_all
if "%choice%"=="2" goto start_backend
if "%choice%"=="3" goto start_frontend
if "%choice%"=="4" goto install_deps
if "%choice%"=="5" goto check_env
if "%choice%"=="6" goto build_frontend
if "%choice%"=="7" goto clean_cache
if "%choice%"=="8" goto exit
echo Invalid option, please try again.
goto menu

:start_all
echo.
echo Starting Full System...
echo.

:: Check dependencies
call :check_dependencies
if errorlevel 1 goto menu

:: Start backend
echo Starting backend service...
start "DataPlexus Backend" cmd /k "cd /d backend && python main.py"
echo Backend service starting... (Port: 8000)

:: Wait for backend to start
echo Waiting for backend service to start...
timeout /t 5 >nul

:: Start frontend
echo Starting frontend service...
start "DataPlexus Frontend" cmd /k "cd /d frontend && npm run dev"
echo Frontend service starting... (Port: 3000)

echo.
echo System startup complete!
echo.
echo Backend API Docs: http://localhost:8000/docs
echo API Management:  http://localhost:8000/redoc
echo Frontend UI:     http://localhost:3000
echo Health Check:    http://localhost:8000/health
echo.
echo Note: Both service windows will remain open. Close windows to stop services.
echo.
pause
goto menu

:start_backend
echo.
echo %BLUE%🔧 启动后端服务...%NC%
echo.

:: 检查Python和依赖
call :check_python
if errorlevel 1 goto menu

cd /d backend
echo %YELLOW%📍 当前目录: %CD%%NC%
echo %YELLOW%🐍 启动FastAPI服务器...%NC%
python main.py
echo.
pause
goto menu

:start_frontend
echo.
echo %BLUE%🎨 启动前端服务...%NC%
echo.

:: 检查Node.js和依赖
call :check_nodejs
if errorlevel 1 goto menu

cd /d frontend
echo %YELLOW%📍 当前目录: %CD%%NC%
echo %YELLOW%⚡ 启动Vite开发服务器...%NC%
npm run dev
echo.
pause
goto menu

:install_deps
echo.
echo %BLUE%📦 安装项目依赖...%NC%
echo.

:: 检查基础环境
call :check_python
if errorlevel 1 goto menu
call :check_nodejs
if errorlevel 1 goto menu

echo %YELLOW%📦 安装后端依赖...%NC%
cd /d backend
pip install -r requirements.txt
if errorlevel 1 (
    echo %RED%❌ 后端依赖安装失败%NC%
    pause
    goto menu
)
echo %GREEN%✅ 后端依赖安装完成%NC%

echo.
echo %YELLOW%📦 安装前端依赖...%NC%
cd /d ..\frontend
npm install
if errorlevel 1 (
    echo %RED%❌ 前端依赖安装失败%NC%
    pause
    goto menu
)
echo %GREEN%✅ 前端依赖安装完成%NC%

echo.
echo %GREEN%✅ 所有依赖安装完成!%NC%
pause
goto menu

:check_env
echo.
echo %BLUE%🔍 环境检查...%NC%
echo.

:: 检查Python
echo %YELLOW%🐍 检查Python环境...%NC%
python --version >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ Python未安装或未添加到PATH%NC%
) else (
    for /f "tokens=2" %%i in ('python --version 2^>^&1') do echo %GREEN%✅ Python版本: %%i%NC%
)

:: 检查Node.js
echo %YELLOW%📦 检查Node.js环境...%NC%
node --version >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ Node.js未安装或未添加到PATH%NC%
) else (
    for /f %%i in ('node --version') do echo %GREEN%✅ Node.js版本: %%i%NC%
)

:: 检查npm
echo %YELLOW%📦 检查npm环境...%NC%
npm --version >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ npm未安装或未添加到PATH%NC%
) else (
    for /f %%i in ('npm --version') do echo %GREEN%✅ npm版本: %%i%NC%
)

:: 检查后端依赖
echo %YELLOW%🔍 检查后端依赖...%NC%
cd /d backend
pip list | findstr fastapi >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ 后端依赖未安装 (FastAPI)%NC%
) else (
    echo %GREEN%✅ 后端依赖已安装%NC%
)

:: 检查前端依赖
echo %YELLOW%🔍 检查前端依赖...%NC%
cd /d ..\frontend
if exist node_modules (
    echo %GREEN%✅ 前端依赖已安装%NC%
) else (
    echo %RED%❌ 前端依赖未安装 (node_modules不存在)%NC%
)

:: 检查数据库连接
echo %YELLOW%🗄️  检查数据库连接...%NC%
cd /d ..\backend
python -c "from app.core.config import settings; print(f'数据库配置: {settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}')" 2>nul
if errorlevel 1 (
    echo %RED%❌ 无法读取数据库配置%NC%
) else (
    echo %GREEN%✅ 数据库配置读取成功%NC%
)

echo.
pause
goto menu

:build_frontend
echo.
echo %BLUE%🏗️ 构建前端项目...%NC%
echo.

call :check_nodejs
if errorlevel 1 goto menu

cd /d frontend
echo %YELLOW%🔨 执行构建...%NC%
npm run build
if errorlevel 1 (
    echo %RED%❌ 前端构建失败%NC%
) else (
    echo %GREEN%✅ 前端构建完成! 输出目录: dist/%NC%
)
echo.
pause
goto menu

:clean_cache
echo.
echo %BLUE%🧹 清理缓存...%NC%
echo.

:: 清理Python缓存
echo %YELLOW%🐍 清理Python缓存...%NC%
for /d /r . %%d in (__pycache__) do @if exist "%%d" rd /s /q "%%d"
del /s /q *.pyc >nul 2>&1
echo %GREEN%✅ Python缓存清理完成%NC%

:: 清理前端缓存
echo %YELLOW%📦 清理前端缓存...%NC%
cd /d frontend
if exist node_modules\.cache rd /s /q node_modules\.cache >nul 2>&1
if exist dist rd /s /q dist >nul 2>&1
if exist .vite rd /s /q .vite >nul 2>&1
echo %GREEN%✅ 前端缓存清理完成%NC%

echo.
echo %GREEN%✅ 缓存清理完成!%NC%
pause
goto menu

:: 函数定义
:check_dependencies
call :check_python
if errorlevel 1 exit /b 1
call :check_nodejs
if errorlevel 1 exit /b 1
exit /b 0

:check_python
python --version >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ Python未安装，请先安装Python 3.8+%NC%
    exit /b 1
)
exit /b 0

:check_nodejs
node --version >nul 2>&1
if errorlevel 1 (
    echo %RED%❌ Node.js未安装，请先安装Node.js 16+%NC%
    exit /b 1
)
exit /b 0

:exit
echo.
echo %CYAN%👋 感谢使用 DataPlexus 数据治理平台!%NC%
echo.
exit /b 0
